import express, { Application } from "express";
import setupRouter from "./router";
import { EntityManager, MikroORM } from "@mikro-orm/mariadb";

const { IS_MYSQL } = process.env;
console.log("🚀 ~ file: main.ts:6 ~ IS_MYSQL:", IS_MYSQL);

let em: EntityManager = {} as EntityManager;
const app: Application = express();

async function main() {
    app.use("/public", express.static("src/public"));
    app.use(express.urlencoded());
    app.use(express.json());

    setupRouter(app);

    const port = 9000;
    app.listen(port, () => {
        console.log("running at http://localhost:" + port);
    });

    const { mysqlUser: user, mysqlPass: pass } = process.env;
    const orm = await MikroORM.init({
        // It is important that entities will point to the compiled JS files, and entitiesTs will point to the TS source files. You should not mix those.
        entities: ["./dist/entities"],
        entitiesTs: ["./src/entities"],

        // default since v4, so not needed to specify explicitly
        // metadataProvider: ReflectMetadataProvider,
        host: "127.0.0.1",
        port: 3306,
        user: user || "root",
        password: pass || "root",
        dbName: "work",

        allowGlobalContext: true,

        debug: true,
    });

    app.em = orm.em;
    em = orm.em;
}

main();

export default app;
